package com.twu.biblioteca;

import java.io.BufferedReader;
import java.io.IOException;
import java.util.ArrayList;

/**
 * Created by olga on 7/06/14.
 */
public class UserManager
{
    private static final String LOGIN_PROMPT = "Please enter your login details: ";
    private static final String LIBRARY_NUMBER_PROMPT = "Library number: ";
    private static final String PASSWORD_PROMPT = "Password: ";
    private ArrayList<User> registeredUsers;
    private User currentUser;

    private static final String NO_USER_LOGGED_IN_PROMPT = "No user is currently logged in.";
    private static final String UNABLE_TO_GER_USER_DETAILS_PROMPT = "Unable to read user details, please try again later.";

    public UserManager()
    {
        registeredUsers = new ArrayList<User>();
        initialiseUserManager();
    }

    private void initialiseUserManager()
    {
        registeredUsers.add(new User("111-1111", "1111", "Andrew", "Smith", "ansrews@gmail.com", "5554546"));
        registeredUsers.add(new User("111-1112", "1112", "John", "White", "jwhite@life.com", "23348546"));
        registeredUsers.add(new User("111-1113", "1113", "Alena", "Sviridova", "as_89@hotmail.com", "3453456"));
        registeredUsers.add(new User("111-1114", "1114", "Milena", "Korke", "milica@hotmail.com", "7839662"));
    }

    //To enable unit testing
    public void setRegisteredUsers( ArrayList<User> newUserList )
    {
        this.registeredUsers = (newUserList == null ) ? new ArrayList<User>() : newUserList;
    }

    public User getCurrentUser()
    {
        return this.currentUser;
    }

    public String promptUserForLoginDetails(BufferedReader reader, String message) throws IOException
    {
        return TerminalReadUtility.getUserInput(reader, message);
    }

    public User getUserDetails()
    {
        TerminalPrintUtility.printStringWithCarriageReturn(LOGIN_PROMPT);
        User result = null;

        try
        {
            BufferedReader reader= TerminalReadUtility.getBufferedReader();
            String userLibNo = promptUserForLoginDetails(reader, LIBRARY_NUMBER_PROMPT);
            String userPwd = promptUserForLoginDetails(reader, PASSWORD_PROMPT);
            result = new User(userLibNo, userPwd);
        }
        catch(IOException e) {
            TerminalPrintUtility.printStringWithEmptyLine(UNABLE_TO_GER_USER_DETAILS_PROMPT);
        }

        return result;
    }

    public boolean checkUserRegistered(User user)
    {
        return user != null && registeredUsers.contains(user);
    }

    public boolean loginUser()
    {
        User toBeLoggedIn = this.getUserDetails();
        if( checkUserRegistered(toBeLoggedIn))
        {
            currentUser = registeredUsers.get(registeredUsers.indexOf(toBeLoggedIn));
            return true;
        }
        return false;
    }

    public void printCurrentUserDetails()
    {
        if( currentUser == null )
            TerminalPrintUtility.printStringWithCarriageReturn(NO_USER_LOGGED_IN_PROMPT);
        else
            currentUser.printUserDetails();
    }
}
